package com.rpoonolly.simplegame;

import java.util.List;
import java.util.Map;

import javax.vecmath.Vector2d;

import android.content.Context;
import android.content.res.Resources;

import com.rpoonolly.simpleandroidgame.R;
import com.rpoonolly.simplegame.core.CollisionDetector;
import com.rpoonolly.simplegame.core.CollisionResponder;
import com.rpoonolly.simplegame.core.InputManager;
import com.rpoonolly.simplegame.core.aabb.AABBCollision;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.core.aabb.DynamicAABBEntity;

public class SimplePlayerProcessor extends SimpleCharacterProcessor {
	private long lastTime;
	private InputManager<SimpleUserAction> inputMngr;
	
	public SimplePlayerProcessor(
		CollisionDetector<AABBEntity, AABBCollision> detector,
		CollisionResponder<AABBCollision> responder,
		InputManager<SimpleUserAction> inputMngr
	) {
		super(detector, responder);
	}

	@Override
	public void process(Map<String, List<AABBEntity>> gameWorld) {
		
		if (lastTime == 0) lastTime = System.currentTimeMillis();
		long dt = System.currentTimeMillis() - lastTime;
		lastTime += dt;
		
		List<AABBEntity> worldObjs = gameWorld.get(Config.ENT_GRP_WORLD_OBJS);
		DynamicAABBEntity player = (DynamicAABBEntity) gameWorld.get(Config.ENT_GRP_PLAYER).get(0);
		
		Vector2d v = player.getDirection();
		v.x = 0;
		v.y = 0;
		
		if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_UP)) {
			v.y = Config.PLAYER_SPEED * dt / 1000;
		} else if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_DOWN)) {
			v.y = -1 * Config.PLAYER_SPEED * dt / 1000;
		}
		
		if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_LEFT)) {
			v.x = -1 * Config.PLAYER_SPEED * dt / 1000;
		} else if (inputMngr.isActionRequested(SimpleUserAction.PLAYER_MOVE_RIGHT)) {
			v.x = Config.PLAYER_SPEED * dt / 1000;
		}
		
		System.out.println("PROCESSOR: player: x: " + player.getX() + " y: " + player.getY() + " dx: " + v.x + " dy: " + v.y);
		
		AABBCollision minCollision = checkWorldCollision(player, worldObjs);
		if (minCollision != null)
			v = collisionResponder.collisionRespond(minCollision);
		
		player.setX((int) (player.getX() + v.x));
		player.setY((int) (player.getY() + v.y));
	}
}
